//
//  ShareClientUserInfo.m
//  ShareCenter
//
//  Created by yutao on 14-2-19.
//  Copyright (c) 2014年 yutao. All rights reserved.
//

#import "ShareClientUserInfo.h"
#import "SFHFKeychainUtils.h"



@implementation ShareClientUserInfo

static ShareClientUserInfo *shareClient = nil;

+(id)defaultShareClientUser
{
    
    return [[self alloc] init];

}

- (ShareClientUserInfo *)userInfoWithServiceType:(NSString *)type
{
    return [self readUserInfoDataFromKeychainWithServiceType:type];
}
- (NSString *)userInfoValueForKey:(NSString *)key serviceType:(NSString *)type
{
    return [SFHFKeychainUtils getPasswordForUsername:key andServiceName:type error:nil];
}
- (BOOL)saveUserInfo:(ShareClientUserInfo *)userInfo serviceType:(NSString *)type
{
    return [self saveUserInfoDataToKeychain:userInfo serviceType:type];
}

- (BOOL)deleteUserInfo:(ShareClientUserInfo *)userInfo serviceType:(NSString *)type
{
    return [self deleteUserInfoDataInKeychain:userInfo serviceType:type];
}

- (BOOL)hasUserInfoWithServiceType:(NSString *)type
{
	return [self userInfoWithServiceType:type].screenName &&[self userInfoWithServiceType:type].iconURL && [self userInfoWithServiceType:type].userDescription;
}


- (BOOL)saveUserInfoDataToKeychain:(ShareClientUserInfo *)userInfo serviceType:(NSString *)type
{
	BOOL	screenNameFlag	= [SFHFKeychainUtils storeUsername:kShareClientUserInfo_screenName andPassword:userInfo.screenName forServiceName:type updateExisting:YES error:nil];
	BOOL	iconURLFlag	= [SFHFKeychainUtils storeUsername:kShareClientUserInfo_iconURL andPassword:userInfo.iconURL forServiceName:type updateExisting:YES error:nil];
	BOOL	descFlag	= [SFHFKeychainUtils storeUsername:kShareClientUserInfo_userDescription andPassword:userInfo.userDescription forServiceName:type updateExisting:YES error:nil];
    BOOL	uidFlag	= [SFHFKeychainUtils storeUsername:kShareClientUserInfo_uid andPassword:userInfo.uid forServiceName:type updateExisting:YES error:nil];
    BOOL	serviceTypeFlag	= [SFHFKeychainUtils storeUsername:kShareClientUserInfo_serviceType andPassword:userInfo.serviceType forServiceName:type updateExisting:YES error:nil];
    
	return screenNameFlag && iconURLFlag && descFlag && uidFlag && serviceTypeFlag;
}

- (ShareClientUserInfo *)readUserInfoDataFromKeychainWithServiceType:(NSString *)type
{
    
	self.screenName = [SFHFKeychainUtils getPasswordForUsername:kShareClientUserInfo_screenName andServiceName:type error:nil];
	self.iconURL			= [SFHFKeychainUtils getPasswordForUsername:kShareClientUserInfo_iconURL andServiceName:type error:nil];
	self.userDescription	= [SFHFKeychainUtils getPasswordForUsername:kShareClientUserInfo_userDescription andServiceName:type error:nil];
    self.uid	= [SFHFKeychainUtils getPasswordForUsername:kShareClientUserInfo_uid andServiceName:type error:nil];
    self.serviceType	= [SFHFKeychainUtils getPasswordForUsername:kShareClientUserInfo_serviceType andServiceName:type error:nil];

	return self;
}

- (BOOL)deleteUserInfoDataInKeychain:(ShareClientUserInfo *)userInfo serviceType:(NSString *)type
{
	BOOL	screenNameFlag		= [SFHFKeychainUtils deleteItemForUsername:kShareClientUserInfo_screenName andServiceName:type error:nil];
	BOOL	iconURLFlag		= [SFHFKeychainUtils deleteItemForUsername:kShareClientUserInfo_iconURL andServiceName:type error:nil];
	BOOL	descFlag		= [SFHFKeychainUtils deleteItemForUsername:kShareClientUserInfo_userDescription andServiceName:type error:nil];
    
    BOOL	uidFlag		= [SFHFKeychainUtils deleteItemForUsername:kShareClientUserInfo_uid andServiceName:type error:nil];
    
    BOOL	serviceTypeFlag		= [SFHFKeychainUtils deleteItemForUsername:kShareClientUserInfo_serviceType andServiceName:type error:nil];
    
	return screenNameFlag && iconURLFlag && descFlag && uidFlag && serviceTypeFlag;
}
@end

@implementation ShareClientUserInfoSina
static ShareClientUserInfoSina *shareClientSina = nil;

+(id)defaultShareClientOAuth
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareClientSina = [[self alloc] init];
    });
    return shareClientSina;
}
@end

@implementation ShareClientUserInfoQQ

static ShareClientUserInfoQQ *shareClientQQ = nil;

+(id)defaultShareClientOAuth
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareClientQQ = [[self alloc] init];
    });
    return shareClientQQ;
}

@end



